<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar bgColor="#fff" placeholde="请输入搜索内容" :radius="20" cancelButton="none" @input="input" ></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="search-list" v-if="searcList">
      <view   class="search-item" v-for="(item, index) in searcList" :key="index" @click="goToDetail(item)">
        <view class="goods-name" >
          {{item.goods_name}}
        </view>
        <uni-icons type="arrow-right" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history" v-if="historyList">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="17" @click="clearHistory"></uni-icons>
      </view>
      <view class=" history-list">
        <uni-tag class='uni-tag' v-for="(item, index) in historyList" :text="item" :inverted="true" @click="goToGoodsList(item)">
        </uni-tag>
      </view>
    </view>
    
    
  </view>
</template>

<script>
    import {$http} from '@escook/request-miniprogram'
  export default {
    data() {
      return {
        timer: null,
        kw: '',
        searcList: [],
        historyList: JSON.parse(uni.getStorageSync('kw')) || []
      }
    },
    methods: {
      // 搜索框输入事件
      input (value) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          console.log(value);
          this.kw = value
          
          // 获取搜索建议数据
          this.getSearchList()
          
          // 存入搜索历史
          if (!this.historyList.includes(value)) {
            this.historyList.unshift(value)
            // console.log('asjmocis')
          }else {
            const index = this.historyList.indexOf(value)
            this.historyList.splice(index, 1)
            this.historyList.unshift(value)
          }
          // 将搜索历史保存到本地
          uni.setStorageSync('kw', JSON.stringify(this.historyList))
        },500)
        
      },
      
      // 异步获取搜索历史
      async getSearchList() {
        if (!this.kw) {
          this.searcList = []
        }
        else {
          const res = await $http.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch', {query: this.kw})
          if (res.data.meta.status !== 200) {
            return uni.showToast({
              title: "数据获取取失败",
              duration:1500,
              icon: 'none'
            })
          }
          console.log(res.data.message);
          this.searcList = res.data.message
          console.log(this.searcList);
        }
      },
      // 点击跳转到商品详情页面
      goToDetail(item) {
        // console.log(item.goods_id);
        uni.navigateTo({
          url: '/subPackge/goodsList/goodsList?id=' + item.goods_id
        })
      },
      // 清空历史记录
      clearHistory() {
        this.historyList = []
        uni.setStorageSync('kw', [])
      },
      // 跳转到商品列表页面
      goToGoodsList (kw) {
        uni.navigateTo({
          url: '/subPackge/goodsList/goodsList?query=' + kw
        })
      }
      
      
      
     }
  }
</script>

<style lang="scss" scoped>
.search-box {
  // height: 100px;
  position: sticky;
  top: 0;
  z-inde: 100;
}
.search-list {
  padding: 0 13px;
  .search-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    padding: 13px 5px;
    border-bottom: 1px solid #efefef;
    .goods-name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
.history {
  margin-top: 10px;
  padding: 0 20px;
  .history-title {
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
 
    
  }
  .history-list {
    display: flex;
    flex-wrap: wrap;
    // margin-top: 20px;

  }
}
.uni-tag {
      margin-top: 20px;
      margin-right: 15px;
    }
</style>
